<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
</body>
<script>
    class TrafficLight {
        color;

        constructor() {
            this.color = 'red';
        }

        async switchColor() {
            await new Promise(resolve => setTimeout(resolve, 3000));
            if (this.color === 'red') {
                this.color = '红色';
            } else if (this.color === 'green') {
                this.color = '绿色';
            } else if (this.color === 'yellow') {
                this.color = '黄色';
            }
        }

        getColor() {
            return this.color;
        }
    }

    let trafficLight = new TrafficLight();

    async function main() {
        while (true) {
            console.log(`当前颜色:${trafficLight.getColor()}`);
            await trafficLight.switchColor();
        }
    }

    main();
</script>

</html>